package cn.ac.iie.util;

import java.io.Serializable;
import org.redisson.Config;
import org.redisson.Redisson;
import org.redisson.RedissonClient;
import org.redisson.connection.balancer.RandomLoadBalancer;

/**
 * 分布式锁客户端
 *
 */
public class RedissonUtil implements Serializable {

    private static RedissonClient redisson = null;
    private final String hosts;
    private final String masterName;
    private final String masterAddress;
    private final int deploymentMode;

    public RedissonClient getClient() {
        if(redisson==null){
                    init(hosts, masterName, masterAddress, deploymentMode);
        }
        return redisson;
    }

    public RedissonUtil(String hosts, String masterName, String masterAddress, int deploymentModel) {
        this.hosts = hosts;
        this.deploymentMode = deploymentModel;
        this.masterAddress = masterAddress;
        this.masterName = masterName;
    }

    public Redisson getSingleClient(String host) {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(host)
                .setConnectionPoolSize(1000);

        Redisson redisson = (Redisson) Redisson.create(config);
        return redisson;
    }

    //Master/Slave servers connection:
    public RedissonClient getMasterSlaveClient(String add, String hosts) {
        Config config = new Config();
        String[] hostarr = hosts.split(",");
        config.useMasterSlaveServers()
                .setMasterAddress(add)
                .setLoadBalancer(new RandomLoadBalancer())
                .addSlaveAddress(hostarr)
                .setMasterConnectionPoolSize(10000)
                .setSlaveConnectionPoolSize(10000);
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }

    //Sentinel servers connection:
    public RedissonClient getSentinelClient(String masterName, String hosts) {
        String[] hostarr = hosts.split(";");
        Config config = new Config();
        config.useSentinelServers()
                .setMasterName(masterName)
                .addSentinelAddress(hostarr)
                .setMasterConnectionPoolSize(10000)
                .setSlaveConnectionPoolSize(10000);

        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }

    //Cluster nodes connections:
    public RedissonClient getClusterClient(String hosts) {
        Config config = new Config();
        String[] hostarr = hosts.split(";");
        config.useClusterServers()
                .setScanInterval(2000) // sets cluster state scan interval
                .addNodeAddress(hostarr)
                .setMasterConnectionPoolSize(10000)
                .setSlaveConnectionPoolSize(10000);
        org.redisson.RedissonClient create = Redisson.create(config);
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }

    private void init(String hosts, String masterName, String masterAddress, int deploymentModel) {
        //redis部署模式 SingleHost 1 MasterSlave 2 Sentinel 3  Cluster 4 
        switch (deploymentModel) {
            case 1:
                redisson = getSingleClient(hosts);
                break;
            case 2:
                redisson = getMasterSlaveClient(masterAddress, hosts);
                break;
            case 3:
                redisson = getSentinelClient(masterName, hosts);
                break;
            case 4:
                redisson = getClusterClient(hosts);
                break;
        }
    }
}
